package com.feng.leetcode;

/**
 * 无重复字符的最长子串
 */
public class Third {
    public static void main(String[] args) {

        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }
    public static int lengthOfLongestSubstring(String s) {
        int l = 0;
        char[] chars = s.toCharArray();
        int j = 1;
        StringBuffer sb = new StringBuffer();
        if (chars.length>0){
            sb.append(chars[0]);
        }
        for (int i=0;i<chars.length;i++){
            if (i==j){
                System.out.println("i= " + i);
                sb.append(chars[i]);
                j=i+1;
            }
            for(;j<chars.length;j++){
                boolean tag = true;
                for (int k=0;k<sb.length();k++){
                    if (sb.charAt(k) == chars[j]){
                        tag = false;
                        break;
                    }
                }
                if (tag){
                    sb.append(chars[j]);
                }else{
                    break;
                }
            }
            System.out.println(sb);

            if (l<sb.length()){
                l = sb.length();
            }
            sb.deleteCharAt(0);
        }
        return l;
    }

    public static int lengthOfLongestSubstring1(String s) {
        int l = 0;
        char[] chars = s.toCharArray();
        for (int i=0;i<chars.length;i++){
            StringBuffer sb = new StringBuffer();
            sb.append(chars[i]);
            for(int j=i+1;j<chars.length;j++){
                boolean tag = true;
                for (int k=0;k<sb.length();k++){
                    if (sb.charAt(k) == chars[j]){
                        tag = false;
                        break;
                    }
                }
                if (tag){
                    sb.append(chars[j]);
                }else{
                    break;
                }
            }
            if (l<sb.length()){
                System.out.println(sb);
                l = sb.length();
            }
        }
        return l;
    }
}
